#include "fileDataHandle.h"




CFileDataHandle::CFileDataHandle()
{
	m_AllLegalClients.clear();
	m_AllClientFastDFSData.clear();
}



CFileDataHandle::~CFileDataHandle()
{
	m_AllLegalClients.clear();
	m_AllClientFastDFSData.clear();
}

void CFileDataHandle::InsertNewClient(const int& clientfd)
{
	m_AllLegalClients.insert(clientfd);
}

void CFileDataHandle::ClearAllClients()
{
	m_AllLegalClients.clear();
}

void CFileDataHandle::DeleteOneClient(const int& clientfd)
{
	m_AllLegalClients.erase(clientfd);
}

void CFileDataHandle::InsertClientFastdfsData(const int& clientfd, const std::shared_ptr<SFastdfsData>& clientData)
{
	m_AllClientFastDFSData.insert(std::make_pair(clientfd, clientData));
}

std::shared_ptr<SFastdfsData> CFileDataHandle::GetClientData(const int& clientfd)
{
	auto clientFastDfsData_iter = m_AllClientFastDFSData.find(clientfd);
	if (clientFastDfsData_iter == m_AllClientFastDFSData.end())
		return nullptr;

	return clientFastDfsData_iter->second;
}

void CFileDataHandle::DeleteOneClientFastdfsData(const int& clientfd)
{
	auto clientFastDfsData_iter = m_AllClientFastDFSData.find(clientfd); 
	if (clientFastDfsData_iter == m_AllClientFastDFSData.end())
		return ;

	m_AllClientFastDFSData.erase(clientFastDfsData_iter);
}

void CFileDataHandle::ClearAllClientsFastdfsData()
{
	m_AllClientFastDFSData.clear();
}

bool CFileDataHandle::HasThisClients(const EpollContext* const epoll_context)
{
	if (epoll_context == nullptr)
	{
		FATAL_MSG("epoll_context is nullptr");	
		return false;
	}

	if (m_AllLegalClients.count(epoll_context->fd) == 1)
	{
		return true;
	}
	else if (m_AllLegalClients.count(epoll_context->fd) > 1)
	{
		ERROR_MSG("can not has many same client at same time lient ip: " << epoll_context->client_ip.c_str() 
				<< ", client fd: " << epoll_context->fd  << " mesg ID: " << epoll_context->uiMessageID);
	}

	return false;
}

